package com.tgh.gles2.c7.multisample;

import com.tgh.gles2.R;
import com.tgh.gles2.c7.cubemap.CubemapRenderer;
import com.tgh.utils.GLShaderManager;
import com.tgh.utils.GLTools;
import com.tgh.utils.Matrix44f;

import android.content.Context;
import android.opengl.GLES20;

import javax.microedition.khronos.opengles.GL10;

public class MultiSampleRenderer extends CubemapRenderer {

    public MultiSampleRenderer(Context context) {
        super(context);
    }

    @Override
    protected void initRelectionProgram() {
        String vpSrc=GLShaderManager.readFileFromAssets(mContext, "MultiSampleReflection.vp");
        String fpSrc=GLShaderManager.readFileFromAssets(mContext, "MultiSampleReflection.fp");
        reflectionShader = GLShaderManager.createProgram(vpSrc, fpSrc,
                new int[]{GLShaderManager.GL_ATTRIBUTE_VERTEX,GLShaderManager.GL_ATTRIBUTE_NORMAL,GLShaderManager.GL_ATTRIBUTE_TEXTURE0},
                new String[]{"vVertex","vNormal","vTexCoords"} );
        locReflMvMatrix = GLES20.glGetUniformLocation(reflectionShader, "mvMatrix");
        locReflPMatrix = GLES20.glGetUniformLocation(reflectionShader, "pMatrix");
        locReflInverseCamera = GLES20.glGetUniformLocation(reflectionShader, "inverseCamera");
        locRefCubeMap = GLES20.glGetUniformLocation(reflectionShader, "cubeMap");
        locRefTarnish = GLES20.glGetUniformLocation(reflectionShader, "tarnishMap");
    }
    @Override
    protected void createTextures() {
        super.createTextures();
       int[] textures = GLTools.genTextures(1);
       tranishTexture=textures[0];
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tranishTexture);
        GLTools.loadDrawableToTexture2D(mContext, 
                R.drawable.tarnish, 
                GLES20.GL_LINEAR_MIPMAP_LINEAR, 
                GLES20.GL_LINEAR, 
                GLES20.GL_CLAMP_TO_EDGE, 
                true);
        
         GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
         GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tranishTexture);
         GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
         GLES20.glBindTexture(GLES20.GL_TEXTURE_CUBE_MAP, cubeTextures);
    }
    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT|GLES20.GL_DEPTH_BUFFER_BIT);
        
        Matrix44f cameraRotOnly = cameraFrame.getCameraMatrix(true);
        Matrix44f camera = cameraFrame.getCameraMatrix(false);
        Matrix44f inverseCamera=new Matrix44f();
        inverseCamera.load(cameraRotOnly);
        inverseCamera.inverse();
        modelViewMatrix.pushMatrix();
            modelViewMatrix.multMatrix(camera);
            GLES20.glUseProgram(reflectionShader);
            GLES20.glUniformMatrix4fv(locReflPMatrix, 1, false, projectionMatrix.getMatrix().getArray(), 0);
            GLES20.glUniformMatrix4fv(locReflMvMatrix, 1, false, modelViewMatrix.getMatrix().getArray(), 0);
            GLES20.glUniformMatrix4fv(locReflInverseCamera, 1, false, inverseCamera.getArray(), 0);
            GLES20.glUniform1i(locRefTarnish, 1);
            GLES20.glUniform1i(locRefCubeMap, 0);
            
            GLES20.glEnable(GLES20.GL_CULL_FACE);
            sphereBatch.draw();
            GLES20.glDisable(GLES20.GL_CULL_FACE);
        modelViewMatrix.popMatrix();
        
        modelViewMatrix.pushMatrix();
            modelViewMatrix.multMatrix(cameraRotOnly);
            GLES20.glUseProgram(skyBoxShader);
            GLES20.glUniformMatrix4fv(locSkyMvpMatrix, 1, false, transformPipleLine.getModelViewProjectionMatrix().getArray(), 0);
            cubeBatch.draw();
        modelViewMatrix.popMatrix();
        
    }
    protected int locRefTarnish;
    protected int locRefCubeMap;
    protected int tranishTexture;
}
